package com.qf.controller;

import com.qf.pojo.ResponseData;
import com.qf.pojo.User;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService ;
    @RequestMapping("/login")
    @ResponseBody
    public ResponseData login( String username, String password){
        System.out.println(username);
        System.out.println(password);
        User user = userService.findUser(username);
        if (user.getPassword()==null){
            return new ResponseData(0,"用户不存在");
        }
        if (!user.getPassword().equals(password)){
            return new ResponseData(0,"密码错误");
        }

        return new ResponseData(1,"恭喜你,登录成功");
    }

    @RequestMapping("/adminregister")
    public String register(){
        return "register";
    }
    @RequestMapping("/add")
    public String add( String username, String password)  {
        System.out.println(username+"+"+password);
        userService.addUser(username,password);
//        request.getRequestDispatcher("index.jsp").forward(request,response);
        return "redirect:/index.jsp";
    }
}
